﻿// Class used to resize the selected image
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace OCRGUI2008
{
    class ImageResize
    {
        public ImageResize()
        {

        }


        public Image resizeImage(Image imgToResize, Size size) // imgToResize is the actual image; size is the current dimensions of the image
        {
            int destWidth = 0;
            int destHeight = 0;
            
            if (imgToResize.Width > size.Width && imgToResize.Height > size.Height)
            {
               

                int sourceWidth = imgToResize.Width;
                int sourceHeight = imgToResize.Height;

                float nPercent = 0;
                float nPercentW = 0;
                float nPercentH = 0;

                nPercentW = ((float)size.Width / (float)sourceWidth);
                nPercentH = ((float)size.Height / (float)sourceHeight);

                if (nPercentH < nPercentW)
                    nPercent = nPercentH;
                else
                    nPercent = nPercentW;

                 destWidth = (int)(sourceWidth * nPercent);
                 destHeight = (int)(sourceHeight * nPercent);
            }
            else
            {
                 destWidth = (int)imgToResize.Width;
                 destHeight = (int)imgToResize.Height;
            }

                Bitmap b = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage((Image)b);
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
                g.Dispose();
          

            return (Image)b;
        }
    }
}
